Quick Start Tutorial

Welcome to quick tutorial on FormulaLab, best environment for your formulas database!

[38]:
import numpy as np
import pandas as pd
import sympy as sp

import FormulaLab as fl

FormulaLab allows you to:

1. Search for formula(s) in two ways:
  1. Direct search.

        `FormulaLab.FormulaSearch.find(function, [variables,])`

  2. Derivation: By going through the formulas database and algebraically
     solve for the desired formula.

        `FormulaLab.FormulaSearch.derive(function, variable)`

2. Convert symbolic formulas into python functions.

      `FormulaLab.FormulaSearch.function(formula:symbolic expr)`
      `FormulaLab.FormulaSearch.find(function, [variables,], function=True)`

3. Call stored formulas in a database by its ID number, and use it anywhere
   in your code with any form.

      `FormulaLab.FormulaSearch.find(function, id)`

Below are four steps of how you may use FormulaLab

1. Import
2. Initiate
3. Search
4. Implement

* FormulaLab is bult upon sympy

Step 1: Importing Formulas from a Database

Data must ba a pandas.DataFrame, list, dict, tuple, or set

[2]:
data_list = ['f=m*a','a=v/t']
data_dict = {'1':'f=m*a','2':'a=v/t'}
data_dict_2 = {'ID':[1, 2], 'Formula':['f=m*a','a=v/t']}
data_tuple = ('f=m*a','a=v/t')
data_set = {'f=m*a','a=v/t'}

data_list
[2]:
['f=m*a', 'a=v/t']

Data from external Database, should be imported as a DataFrame

This is the recommended way of storing and loading formulas, and pandas is
one of the best python packages that can handle this.
[3]:
df = pd.read_csv('Quick Database.csv')
df
[3]:
ID Formula
0 1 d = v * t
1 2 a = v / t
2 3 f = m * a
3 5 p = m * v

Step 2: Initiate the formula search engine

[4]:
phyfos = fl.FormulaSearch(data=data_list)
phyfos.data
# The new "Args" column is automatically generated
# to help speed the search algorithm.
[4]:
ID Formula Args
0 1 f=m*a [a, m, f]
1 2 a=v/t [v, t, a]
[5]:
phyfos = fl.FormulaSearch(data=data_dict)
phyfos.data
[5]:
ID Formula Args
0 1 f=m*a [a, m, f]
1 2 a=v/t [v, t, a]
[6]:
phyfos = fl.FormulaSearch(data=df, formula_col='Formula', id_col='ID')
#formula_col and id_col values should mathch the formulas database
phyfos.data
[6]:
ID Formula Args
0 1 d = v * t [v, t, d]
1 2 a = v / t [v, t, a]
2 3 f = m * a [a, m, f]
3 5 p = m * v [p, m, v]

Step 3: Search for formula(s)

1. Direct search

[7]:
d = phyfos.find('d')  #Searching is case sensitivity
d
[7]:
[t*v]
[8]:
v = phyfos.find('v')
v
[8]:
[d/t, a*t, p/m]
[9]:
v_as_a_function_of_a = phyfos.find('v', 'a')
v_as_a_function_of_a
[9]:
[a*t]
[10]:
a_as_a_function_of_v = phyfos.find('a', 'v')
a_as_a_function_of_v
[10]:
[v/t]
[11]:
v_as_a_function_of_t_d = phyfos.find('v', ['t','d'])
v_as_a_function_of_t_d
[11]:
[d/t]

2. Search by formula ID number

Each formula has a fixed id number that should not be changed. It
is recomeneded to use this method when you know what formula to use
in your code. It is faster than direct search, and the later could
give you different result when the database is expanded or changed.
[12]:
f = phyfos.find('f', id=3)
f
[12]:
[a*m]
[13]:
# Say you want "m" to be the function, you do not have to rewrite your formula again!
m = phyfos.find('m', id=3)
m
[13]:
[f/a]
[14]:
# Also for 'a'. This is very helpful when there is a mistake
# in the formula! You only change it once in your database.
a = phyfos.find('a', id=3)
a
# You will see how to convert this to a python function in step 4
[14]:
[f/m]

2. Deriving formulas, through algabric substitutions

This is the most interesting part! FormulaLab can do algebraic
manibulations including integrals and derivatives using the power
of sympy!
[15]:
phyfos.data
[15]:
ID Formula Args
0 1 d = v * t [v, t, d]
1 2 a = v / t [v, t, a]
2 3 f = m * a [a, m, f]
3 5 p = m * v [p, m, v]
[16]:
phyfos.find('d', 'a') # What if you want to know what is the d(a)??
#It is not in the database! So, find() is not helpful here, derive() is!
[16]:
[]
[17]:
d = phyfos.derive('d', 'a', shortest_path=True)
# shortest_path=True is faster and the default.
d
[17]:
[a*t**2, v**2/a, a*p*t/f]
[18]:
# You can also see all of your traces:
phyfos.traces
[18]:
[[1, 'v', 2], [1, 't', 2], [1, 'v', 5, 'm', 3]]
[19]:
d = phyfos.derive('d', 'a', shortest_path=False)
d
# Sometimes you get more solutions when you turn off shortest_path,
# because derive() tries with all possible paths, with no shortcuts!
[19]:
[a*t**2, v**2/a, a*p*t/f]
[20]:
# You can also see the all of your traces:
phyfos.traces
# The extra path has a repetitive solution
[20]:
[[1, 'v', 2], [1, 't', 2], [1, 'v', 5, 'm', 3], [1, 't', 2, 'v', 5, 'm', 3]]
[21]:
# You can pretty print your solutions
# Latex print
d[1]
[21]:
$\displaystyle \frac{v^{2}}{a}$
[22]:
sp.pretty_print(d[1])
 2
v
──
a

Step 4: Use your formulas

Once you get the formula(s) you want, you can use them in many different ways:
1. Use them as a python functions.
2. Interact with them in their symbolic form using sympy.

1. Converting formulas expresion to a python functions

[23]:
phyfos.data
[23]:
ID Formula Args
0 1 d = v * t [v, t, d]
1 2 a = v / t [v, t, a]
2 3 f = m * a [a, m, f]
3 5 p = m * v [p, m, v]
[24]:
f = phyfos.find('f', function=True)
f(m=10, a=2)
[24]:
20
[25]:
a = phyfos.find('a',id=3, function=True)
a(f=20, m=10)
[25]:
2.0
[26]:
# You can also convert symbolic expression to python function
d = phyfos.derive('d', 'a')[0]
d
[26]:
$\displaystyle a t^{2}$
[27]:
d_func = phyfos.function(d)
d_func(t=2, a=1)
[27]:
4

2. Use your formulas with sympy

For more information about how to use sympy, visit: SymPy

[28]:
d
[28]:
$\displaystyle a t^{2}$
[29]:
sp.var('a x')
d = d.subs(a,sp.sin(x))
d
[29]:
$\displaystyle t^{2} \sin{\left(x \right)}$
[30]:
d.series(x)
[30]:
$\displaystyle t^{2} x - \frac{t^{2} x^{3}}{6} + \frac{t^{2} x^{5}}{120} + O\left(x^{6}\right)$
[31]:
d.diff(x) # take the derivative of d with respect to x
[31]:
$\displaystyle t^{2} \cos{\left(x \right)}$

Else can be done with FormulaLab

[32]:
#Find formula as string, by its id number
str_fo = phyfos.find_raw_formula(1)
str_fo
[32]:
'd = v * t'
[33]:
# Convert str expr to formula and solve for a variable
phyfos.solve_for(expr=str_fo, var='v')
[33]:
[d/t]
[34]:
# Tels you where to find certain variable in your database
phyfos.get_formula_ids('v')
[34]:
[1, 2, 5]
[35]:
phyfos.get_formula_ids(('v','a')) # --> 'v' and 'a' can be found in id = 2
[35]:
[2]
[36]:
# What if you want to know how your formulas are connected!
phyfos.trace([5,1,2])
[36]:
[[5, 'v', 1, 'v', 2], [5, 'v', 1, 't', 2]]
[37]:
# Once you are done, you can see all your derived formulas stored in
phyfos.all_derived_formulas
[37]:
{a*p*t/f, a*t**2, v**2/a}